var warFrameApi = 'https://api.warframestat.us';
var requestTime = 15000;

$(document).ready(function() {
    night_spirit();
    venus();
    $('#clan_editor').summernote({
        toolbar: [
            ['style', ['bold', 'italic', 'underline', 'clear']],
            ['font', ['strikethrough', 'superscript', 'subscript']],
            ['fontsize', ['fontsize']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']]
        ],
        popover: false,
        height: 200,
        lang: 'zh-CN',
        placeholder: '在这里编辑详细描述...',
        shortcuts: false
    })
});

setInterval(function(){
    night_spirit();
    venus();
}, requestTime);

function venus()
{
    $.ajax({
        url:warFrameApi+'/pc/vallisCycle',
        type:"get",
        dataType:"json",
        data:{},
        success:function(returnData){
            var timeLeft = returnData['timeLeft'].substr(0, 3);
            $('#venus_time').text(timeLeft);
            if (returnData['isWarm'] == true)
            {
                $('#venus_state').text('寒冷');
            }else {
                $('#venus_state').text('温暖');
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function night_spirit()
{
    $.ajax({
        url:warFrameApi+'/pc/cetusCycle',
        type:"get",
        dataType:"json",
        data:{},
        success:function(returnData){
            var timeLeft = returnData['timeLeft'].substr(0, 3);
            $('#night_spirit_time').text(timeLeft);
            if (returnData['isDay'] == true)
            {
                $('#night_spirit_state').text('天黑');
            }else {
                $('#night_spirit_state').text('天亮');
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function copyUrl()
{
    var url = window.location.href;
    var urlInput = document.createElement('input');
    urlInput.value = url;
    document.body.appendChild(urlInput);
    urlInput.select();
    document.execCommand("Copy");
    urlInput.className = 'urlInput';
    urlInput.style.display='none';
    layer.msg('OK,已获取本页地址,去粘贴分享给你的朋友吧！');
}

function copyText(id) {
    var input = document.getElementById("copy"+id);
    input.select();
    document.execCommand("copy");
    layer.msg('复制成功！在游戏中按 T 粘贴信息联系对方吧！');
}

function jumpUrl(url) {
    window.location.href = url;
}

function setButtonDisabled(buttonName) {
    $(buttonName).attr({ disabled: "disabled" });
}

function removeButtonDisabled(buttonName) {
    $(buttonName).removeAttr("disabled");
}

function deleteRiven(id, number)
{
    $.ajax({
        url:'/deleteRiven',
        type:"post",
        dataType:"json",
        data:{
            id:id
        },
        beforeSend: function () {
            layer.msg('操作中...')
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('删除成功！');
                    $('#de'+number).addClass('flipOutX');
                    setTimeout(function(){
                        $('#de'+number).remove();
                    }, 1000);
                    break;
                default:
                    layer.msg(returnData);
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function deleteClanActivity(id, number)
{
    $.ajax({
        url:'/deleteClanActivity',
        type:"post",
        dataType:"json",
        data:{
            id:id
        },
        beforeSend: function () {
            layer.msg('操作中...')
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('删除成功！');
                    $('#de'+number).addClass('flipOutX');
                    setTimeout(function(){
                        $('#de'+number).remove();
                    }, 1000);
                    break;
                default:
                    layer.msg(returnData);
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function deleteOther(id, number)
{
    $.ajax({
        url:'/deleteOther',
        type:"post",
        dataType:"json",
        data:{
            id:id
        },
        beforeSend: function () {
            layer.msg('操作中...')
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('删除成功！');
                    $('#de'+number).addClass('flipOutX');
                    setTimeout(function(){
                        $('#de'+number).remove();
                    }, 1000);
                    break;
                default:
                    layer.msg(returnData);
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function deletePrime(id, number)
{
    $.ajax({
        url:'/deletePrime',
        type:"post",
        dataType:"json",
        data:{
            id:id
        },
        beforeSend: function () {
            layer.msg('操作中...')
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('删除成功！');
                    $('#de'+number).addClass('flipOutX');
                    setTimeout(function(){
                        $('#de'+number).remove();
                    }, 1000);
                    break;
                default:
                    layer.msg(returnData);
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function deleteMod(id, number)
{
    $.ajax({
        url:'/deleteMod',
        type:"post",
        dataType:"json",
        data:{
            id:id
        },
        beforeSend: function () {
            layer.msg('操作中...')
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('删除成功！');
                    $('#de'+number).addClass('flipOutX');
                    setTimeout(function(){
                        $('#de'+number).remove();
                    }, 1000);
                    break;
                default:
                    layer.msg(returnData);
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_goods_entry').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var account = $.base64.encode($('input[name="account"]').val());
        var password = $.base64.encode($('input[name="password"]').val());
        var name = $('input[name="name"]').val();
        var english_name = $('input[name="english_name"]').val();
        var aka = $('input[name="aka"]').val();
        var des = $('input[name="des"]').val();
        var tax = $('input[name="tax"]').val();
        var price = $('input[name="price"]').val();
        sendGoodsEntry(token, account, password, name, english_name, aka, des, tax, price, '#send_goods_entry');
    }
);

function sendGoodsEntry(token, account, password, name, english_name, aka, des, tax, price, buttonName)
{
    $.ajax({
        url:'/sendGoodsEntry',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            account:account,
            password:password,
            name:name,
            english_name:english_name,
            aka:aka,
            des:des,
            tax:tax,
            price:price
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('操作成功');
                    removeButtonDisabled(buttonName);
                    removeSendGoodsInputVal();
                    $(buttonName).text('确认无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeSendGoodsInputVal();
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function removeSendGoodsInputVal()
{
    $('input[name="name"]').val('');
    $('input[name="english_name"]').val('');
    $('input[name="aka"]').val('');
    $('input[name="des"]').val('');
    $('input[name="tax"]').val('');
    $('input[name="price"]').val('');
}

$('#send_clan_activity').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var title = $('input[name="title"]').val();
        var activity_time = $('input[name="activity_time"]').val();
        var content = $('#clan_editor').summernote('code');
        sendClanActivity(token, title, activity_time, content, '#send_clan_activity');
    }
);

function sendClanActivity(token, title, activity_time, content, buttonName)
{
    $.ajax({
        url:'/sendClanActivity',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            title:title,
            activity_time:activity_time,
            content:content
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('操作成功');
                    removeButtonDisabled(buttonName);
                    $('input[name="title"]').val('');
                    $('input[name="activity_time"]').val('');
                    $('#clan_editor').summernote('code', '');
                    $(buttonName).text('确认无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_comment_content_to_email').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var comment_content = $('input[name="comment_content"]').val();
        var receive_user_id = $('input[name="comment_user_id"]').val();
        sendCommentContentToEmail(token, comment_content, receive_user_id, '#send_comment_content_to_email');
    }
);

function sendCommentContentToEmail(token, comment_content, receive_user_id, buttonName)
{
    $.ajax({
        url:'/sendUserCommentToEmail',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            comment_content:comment_content,
            receive_user_id:receive_user_id
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            layer.msg('发送中..')
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('操作成功');
                    removeButtonDisabled(buttonName);
                    $('input[name="comment_content"]').val('');
                    $(buttonName).text('留言内容发送至此用户邮箱');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('留言内容发送至此用户邮箱');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_comment_content').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var comment_user_id = $('input[name="comment_user_id"]').val();
        var comment_content = $('input[name="comment_content"]').val();
        sendUserComment(token, comment_user_id, comment_content, '#send_comment_content');
    }
);

function sendUserComment(token, comment_user_id, comment_content, buttonName)
{
    $.ajax({
        url:'/sendComment',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            comment_user_id:comment_user_id,
            comment_content:comment_content
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('操作成功');
                    removeButtonDisabled(buttonName);
                    $('input[name="comment_content"]').val('');
                    $(buttonName).text('给此用户留言');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('给此用户留言');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#edit_user_file').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var tag_1 = $('input[name="tag_1"]').val();
        var tag_2 = $('input[name="tag_2"]').val();
        var tag_3 = $('input[name="tag_3"]').val();
        var message = $('input[name="message"]').val();
        editUserFile(token, tag_1, tag_2, tag_3, message, '#edit_user_file');
    }
);

function editUserFile(token, tag_1, tag_2, tag_3, message, buttonName)
{
    $.ajax({
        url:'/updateUserFile',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            tag_1:tag_1,
            tag_2:tag_2,
            tag_3:tag_3,
            message:message
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('操作成功');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#edit_other').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var id = $('input[name="id"]').val();
        var stuff_name = $('input[name="stuff_name"]').val();
        var unit_price = $('input[name="unit_price"]').val();
        var num = $('input[name="num"]').val();
        editOther(token, stuff_name, unit_price, num, id, '#edit_other');
    }
);

function editOther(token, stuff_name, unit_price, num, id, buttonName)
{
    $.ajax({
        url:'/editOther',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            stuff_name:stuff_name,
            unit_price:unit_price,
            id:id,
            num:num
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('修改成功');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#edit_riven').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var id = $('input[name="id"]').val();
        var stuff_name = $('input[name="stuff_name"]').val();
        var unit_price = $('input[name="unit_price"]').val();
        var num = $('input[name="num"]').val();
        var rank = $('input[name="rank"]').val();
        var entry_1 = $('input[name="entry_1"]').val();
        var entry_2 = $('input[name="entry_2"]').val();
        var entry_3 = $('input[name="entry_3"]').val();
        var entry_4 = $('input[name="entry_4"]').val();
        editRiven(token, stuff_name, unit_price, num, id, rank, entry_1, entry_2, entry_3, entry_4, '#edit_riven');
    }
);

function editRiven(token, stuff_name, unit_price, num, id, rank, entry_1, entry_2, entry_3, entry_4, buttonName)
{
    $.ajax({
        url:'/editRiven',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            stuff_name:stuff_name,
            unit_price:unit_price,
            id:id,
            num:num,
            rank:rank,
            entry_1:entry_1,
            entry_2:entry_2,
            entry_3:entry_3,
            entry_4:entry_4
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('修改成功');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#edit_prime').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var id = $('input[name="id"]').val();
        var stuff_name = $('input[name="stuff_name"]').val();
        var unit_price = $('input[name="unit_price"]').val();
        var num = $('input[name="num"]').val();
        editPrime(token, stuff_name, unit_price, num, id, '#edit_prime');
    }
);

function editPrime(token, stuff_name, unit_price, num, id, buttonName)
{
    $.ajax({
        url:'/editPrime',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            stuff_name:stuff_name,
            unit_price:unit_price,
            num:num,
            id:id
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('修改成功');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#edit_mod').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var id = $('input[name="id"]').val();
        var stuff_name = $('input[name="stuff_name"]').val();
        var unit_price = $('input[name="unit_price"]').val();
        var num = $('input[name="num"]').val();
        var rank = $('input[name="rank"]').val();
        editMod(token, stuff_name, unit_price, num, rank, id, '#edit_mod');
    }
);

function editMod(token, stuff_name, unit_price, num, rank, id, buttonName)
{
    $.ajax({
        url:'/editMod',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            stuff_name:stuff_name,
            unit_price:unit_price,
            rank:rank,
            num:num,
            id:id
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('修改成功');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认修改无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_other').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var stuff_name = $('input[name="stuff_name"]').val();
        var unit_price = $('input[name="unit_price"]').val();
        var num = $('input[name="num"]').val();
        var type = $('input[name="type"]').val();
        var url = '';
        switch(type)
        {
            case 1:
                url = '/sendOther';
                break;
            case 2:
                url = '/sendGold';
                break;
            case 3:
                url = '/sendSilver';
                break;
            case 4:
                url = '/sendArcane';
                break;
            case 5:
                url = '/sendSculpt';
                break;
            case 6:
                url = '/sendKuWa';
                break;
            case 7:
                url = '/sendKuBo';
                break;
        }
        sendOther(url, token, stuff_name, unit_price, num, type, '#send_other');
    }
);

function sendOther(url, token, stuff_name, unit_price, num, type, buttonName)
{
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            stuff_name:stuff_name,
            unit_price:unit_price,
            type:type,
            num:num,
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('提交成功');
                    removeButtonDisabled(buttonName);
                    $('input[name="stuff_name"]').val('');
                    $(buttonName).text('确认无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_riven').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var stuff_name = $('input[name="stuff_name"]').val();
        var unit_price = $('input[name="unit_price"]').val();
        var num = $('input[name="num"]').val();
        var rank = $('input[name="rank"]').val();
        var entry_1 = $('input[name="entry_1"]').val();
        var entry_2 = $('input[name="entry_2"]').val();
        var entry_3 = $('input[name="entry_3"]').val();
        var entry_4 = $('input[name="entry_4"]').val();
        sendRiven(token, stuff_name, unit_price, num, rank, entry_1, entry_2, entry_3, entry_4, '#send_riven');
    }
);

function sendRiven(token, stuff_name, unit_price, num, rank, entry_1, entry_2, entry_3, entry_4, buttonName)
{
    $.ajax({
        url:'/sendRiven',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            stuff_name:stuff_name,
            unit_price:unit_price,
            rank:rank,
            entry_1:entry_1,
            num:num,
            entry_2:entry_2,
            entry_3:entry_3,
            entry_4:entry_4,
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('提交成功');
                    removeButtonDisabled(buttonName);
                    $('input[name="stuff_name"]').val('');
                    $('input[name="entry_4"]').val('');
                    $('input[name="entry_3"]').val('');
                    $('input[name="entry_2"]').val('');
                    $('input[name="entry_1"]').val('');
                    $(buttonName).text('确认无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_war_frame').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var stuff_name = $('input[name="stuff_name"]').val();
        var unit_price = $('input[name="unit_price"]').val();
        var num = $('input[name="num"]').val();
        sendWarFrame(token, stuff_name, unit_price, num, '#send_war_frame');
    }
);

function sendWarFrame(token, stuff_name, unit_price, num, buttonName)
{
    $.ajax({
        url:'/sendWarFrame',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            stuff_name:stuff_name,
            unit_price:unit_price,
            num:num
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('提交成功');
                    removeButtonDisabled(buttonName);
                    $('input[name="stuff_name"]').val('');
                    $(buttonName).text('确认无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_weapon').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var stuff_name = $('input[name="stuff_name"]').val();
        var unit_price = $('input[name="unit_price"]').val();
        var num = $('input[name="num"]').val();
        sendWeapon(token, stuff_name, unit_price, num, '#send_weapon');
    }
);

function sendWeapon(token, stuff_name, unit_price, num, buttonName)
{
    $.ajax({
        url:'/sendWeapon',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            stuff_name:stuff_name,
            unit_price:unit_price,
            num:num
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('提交成功');
                    removeButtonDisabled(buttonName);
                    $('input[name="stuff_name"]').val('');
                    $(buttonName).text('确认无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_mod').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var stuff_name = $('input[name="stuff_name"]').val();
        var unit_price = $('input[name="unit_price"]').val();
        var num = $('input[name="num"]').val();
        var rank = $('input[name="rank"]').val();
        sendMod(token, stuff_name, unit_price, num, rank, '#send_mod');
    }
);

function sendMod(token, stuff_name, unit_price, num, rank, buttonName)
{
    $.ajax({
        url:'/sendMod',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            stuff_name:stuff_name,
            unit_price:unit_price,
            num:num,
            rank:rank
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('提交成功');
                    removeButtonDisabled(buttonName);
                    $('input[name="stuff_name"]').val('');
                    $(buttonName).text('确认无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#editPassword').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var old_password = $.base64.encode($('input[name="old_password"]').val());
        var new_password = $.base64.encode($('input[name="new_password"]').val());
        var emailCode = $('input[name="val_code"]').val();
        var email = $('input[name="email"]').val();
        editPassword(token, old_password, new_password, emailCode, '#editPassword', email);
    }
);

function editPassword(token, old_password, new_password, emailCode, buttonName, email)
{
    if (emailCode != returnEmailCode)
    {
        layer.msg('验证码不正确！请检查输入！');
        return false;
    }
    if (email != emailAddress)
    {
        layer.msg('别搞这些没有用的！');
        return false;
    }
    $.ajax({
        url:'/editPassword',
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            old_password:old_password,
            new_password:new_password,
            email_code:emailCode
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 500:
                    layer.msg('操作失败！旧密码不正确！');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击修改并提交');
                    break;
                case 200:
                    $(buttonName).text('大佬！修改成功！！页面跳转中...');
                    setTimeout(function(){
                        jumpUrl('/signIn')
                    }, 800);
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击修改并提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#updateGameState').on('click',function()
    {
        updateGameState();
    }
);

function updateGameState() {
    $.ajax({
        url:"/updateUserGameState",
        type:"post",
        data:{},
        success:function(data){
            switch (data['state'])
            {
                case 201:
                    $('.user_game_state').css("color", "red");
                    $('.user_game_state').text('OFFLINE');
                    layer.msg('修改状态为：OFFLINE');
                    break;
                default:
                    $('.user_game_state').css("color", "forestgreen");
                    $('.user_game_state').text('ONLINE');
                    layer.msg('修改状态为：ONLINE');
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#user_login_out').on('click',function()
    {
        loginOutAjax()
    }
);

$('input[name="email"]').bind('input propertychange', function(){
    var content = $('input[name="email"]').val();
    if (content)
    {
        removeButtonDisabled('#send_email_code');
    }
});

$('#editGameId').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var gameId = $('input[name="new_game_id"]').val();
        var emailCode = $('input[name="val_code"]').val();
        var email = $('input[name="email"]').val();
        editGameId('/updateGameId', token, gameId, emailCode, '#editGameId', email)
    }
);

function editGameId(url, token, gameId, emailCode, buttonName, email)
{
    if (emailCode != returnEmailCode)
    {
        layer.msg('验证码不正确！请检查输入！');
        return false;
    }
    if (email != emailAddress)
    {
        layer.msg('别搞这些没有用的！');
        return false;
    }
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            game_id:gameId,
            email_code:emailCode
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 602:
                    layer.msg('已有相同游戏 ID');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击修改并提交');
                    break;
                case 200:
                    $(buttonName).text('大佬！修改成功！！页面跳转中...');
                    setTimeout(function(){
                        jumpUrl('/')
                    }, 1000);
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击修改并提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#signInButton').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var account = $('input[name="account"]').val();
        var password = $.base64.encode($('input[name="password"]').val());
        signInAction('/signIn', token, account, password, '#signInButton')
    }
);

function signInAction(url, token, account, password, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            account:account,
            password:password
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 500:
                    layer.msg('账号或者密码错误！');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击登录');
                    break;
                case 601:
                    layer.msg('您登录的账号因为发布垃圾信息被管理员封禁！');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击登录');
                    break;
                case 200:
                    $(buttonName).text('大佬！登录成功！!页面跳转中...');
                    setTimeout(function(){
                        jumpUrl('/')
                    }, 800);
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击登录');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#forgetPasswordButton').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var email = $('input[name="email"]').val();
        var password = $.base64.encode($('input[name="password"]').val());
        var emailCode = $('input[name="email_code"]').val();
        forgetPasswordAction('/forgetPassword', token, email, password, emailCode, '#forgetPasswordButton')
    }
);

function forgetPasswordAction(url, token, email, password, emailCode, buttonName)
{
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            email:email,
            password:password,
            email_code:emailCode,
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    $(buttonName).text('重置密码成功！跳转中...');
                    setTimeout(function(){
                        jumpUrl('/signIn')
                    }, 800);
                    break;
                case 503:
                    layer.msg('无此账号！请检查输入');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
                    break;
                case 504:
                    layer.msg('邮箱验证码不正确！');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
                    break;
                case 505:
                    layer.msg('别闹哦！');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('确认无误提交');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

var returnEmailCode = '';
var emailAddress = '';

$('#signUpButton').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var email = $('input[name="email"]').val();
        var gameId = $('input[name="game_id"]').val();
        var password = $.base64.encode($('input[name="password"]').val());
        var emailCode = $('input[name="email_code"]').val();
        signUpAction('/signUp', token, email, gameId, password, emailCode, '#signUpButton')
    }
);

function signUpAction(url, token, email, gameId, password, emailCode, buttonName)
{
    if (emailCode != returnEmailCode)
    {
        layer.msg('验证码不正确！请检查输入！');
        return false;
    }
    if (email != emailAddress)
    {
        layer.msg('注册邮箱地址与发送验证码地址不一致！');
        return false;
    }
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            email:email,
            game_id:gameId,
            password:password,
            email_code:emailCode,
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('操作中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    $(buttonName).text('注册成功了哦！大佬！！');
                    setTimeout(function(){
                        jumpUrl('/signIn')
                    }, 800);
                    break;
                case 601:
                    layer.msg('邮箱已经有人注册过了！');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击注册');
                    break;
                case 602:
                    layer.msg('已有相同的游戏 ID！');
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击注册');
                    break;
                default:
                    layer.msg(returnData);
                    removeButtonDisabled(buttonName);
                    $(buttonName).text('点击注册');
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_forget_password_email_code').on('click',function()
    {
        var email = $('input[name="email"]').val();
        var token = $('input[name="__token__"]').val();
        sendForgetPasswordEmailCode('/sendForgetPasswordEmail', token, email, '#send_forget_password_email_code')
    }
);

function sendForgetPasswordEmailCode(url, token, email, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            email_address:email,
            __token__:token
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('发送中');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    returnEmailCode = returnData['email_code'];
                    emailAddress = email;
                    $(buttonName).text('已发送至您的邮箱！');
                    break;
                default:
                    layer.msg(returnData);
                    $(buttonName).text('点击发送验证码');
                    removeButtonDisabled(buttonName);
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_email_code').on('click',function()
    {
        var email = $('input[name="email"]').val();
        var token = $('input[name="__token__"]').val();
        sendEmailCode('/sendEmail', token, email, '#send_email_code')
    }
);

function sendEmailCode(url, token, email, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            email_address:email,
            __token__:token
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $(buttonName).text('发送中');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    returnEmailCode = returnData['email_code'];
                    emailAddress = email;
                    $(buttonName).text('已发送至您的邮箱！');
                    break;
                default:
                    layer.msg(returnData);
                    $(buttonName).text('点击发送验证码');
                    removeButtonDisabled(buttonName);
            }
        },
        complete: function () {
            getToken();
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function loginOutAjax() {
    $.ajax({
        url:"/signOut",
        type:"post",
        data:{},
        beforeSend: function () {
            setButtonDisabled("#user_login_out");
        },
        success:function(){
            jumpUrl('/')
        },
        complete: function () {
            removeButtonDisabled("#user_login_out");
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#query_goods_entry').on('click',function()
    {
        var token = $('input[name="__token__"]').val();
        var name = $('input[name="name"]').val();
        queryGoodsEntry('/queryGoodsEntry', token, name,'#query_goods_entry')
    }
);

function removeQueryGoodsEntryList() {
    $('#query_list').html('');
}

function queryGoodsEntry(url, token, name, buttonName)
{
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            __token__:token,
            name:name
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            $('#query_goods_entry').val('查询中...');
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    var contentStr = "";
                    if (returnData['data'][0] != null)
                    {
                        removeQueryGoodsEntryList();
                        $.each(returnData['data'], function(index, item){
                            contentStr += '<tr class="animated flipInX">';
                            contentStr += '<td><b>'+item['english_name']+'</b></td>';
                            contentStr += '<td>'+item['name']+'</td>';
                            if (item['price'] != null)
                            {
                                contentStr += '<td><b style="color: red">'+item['price']+'</b> Platinum</td>';
                            }else {
                                contentStr += '<td>暂无</td>';
                            }
                            if (item['des'] != null)
                            {
                                contentStr += '<td>'+item['des']+'</td>';
                            }else {
                                contentStr += '<td>暂无</td>';
                            }
                            contentStr += '<td>'+item['tax']+'</td>';
                            contentStr += '<td>'+item['update_time']+'</td>';
                            contentStr += '</tr>';
                        });
                        $('#query_list').prepend(contentStr);
                        break;
                    }else {
                        removeQueryGoodsEntryList();
                        contentStr += '<tr class="animated flipInX">';
                        contentStr += '<td><b>NULL</b></td>';
                        contentStr += '<td>暂无</td>';
                        contentStr += '<td>暂无</td>';
                        contentStr += '<td>暂无</td>';
                        contentStr += '<td>暂无</td>';
                        contentStr += '<td>暂无</td>';
                        contentStr += '</tr>';
                        $('#query_list').prepend(contentStr);
                        layer.msg('没有查到相关数据！反馈问题请上 留言板 ！');
                        break;
                    }
                default:
                    layer.msg(returnData);
            }
        },
        complete: function () {
            getToken();
            $('input[name="name"]').val('');
            removeButtonDisabled(buttonName);
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

$('#send_message_content').on('click',function()
    {
        var content = $('#message_content').val();
        var token = $('input[name="__token__"]').val();
        messageAjax('/sendMessage', content, token,'#send_message_content')
    }
);

setButtonDisabled('#send_message_content');

$('#message_content').bind('input propertychange', function(){
    var content = $("#message_content").val();
    if (content)
    {
        removeButtonDisabled('#send_message_content');
    }
});

function getToken() {
    $.ajax({
        url:"/getToken",
        type:"post",
        dataType:"json",
        data:{
            key:'LONG'
        },
        success:function(returnData){
            switch (returnData)
            {
                case 500:
                    layer.msg('内部错误');
                    break;
                default:
                    $('input[name="__token__"]').val(returnData);
            }
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}

function messageAjax(url, content, token, buttonName) {
    $.ajax({
        url:url,
        type:"post",
        dataType:"json",
        data:{
            content:content,
            __token__:token
        },
        beforeSend: function () {
            setButtonDisabled(buttonName);
            layer.msg('操作中...')
        },
        success:function(returnData){
            switch (returnData['state'])
            {
                case 200:
                    layer.msg('操作成功！');
                    var contentStr = '<tr class="animated flipInX">';
                    contentStr += '<th scope="col" style="font-weight: 500;">'+returnData['game_id']+'</th>';
                    contentStr += '<th scope="col">'+content+'</th>';
                    contentStr += '<th scope="col">'+returnData['now_time']+'</th>';
                    contentStr += '</tr>';
                    $('#message_content_list').prepend(contentStr);
                    break;
                default:
                    layer.msg(returnData);
            }
        },
        complete: function () {
            getToken();
            $('#message_content').val('');
        },
        error:function(){
            layer.msg('网络异常..')
        }
    });
}